/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.farent.solutions.plugins.stripes.hyperstripes.renders.dojo;

import com.farent.solutions.plugins.stripes.hyperstripes.annotations.RenderKitNamespace;
import com.farent.solutions.plugins.stripes.hyperstripes.core.ComponentAnnotationProcessor;
import com.farent.solutions.plugins.stripes.hyperstripes.core.ComponentModel;
import com.farent.solutions.plugins.stripes.hyperstripes.core.ComponentTagHandler;
import com.farent.solutions.plugins.stripes.hyperstripes.core.ModelProvider;
import com.farent.solutions.plugins.stripes.hyperstripes.core.StripesApplicationContext;
import com.farent.solutions.plugins.stripes.hyperstripes.spi.AbstractRenderKit;
import com.farent.solutions.plugins.stripes.hyperstripes.spi.RenderKit;
import com.farent.solutions.plugins.stripes.hyperstripes.spi.WebLibrary;
import java.util.List;

/**
 *
 * @author user
 */
@RenderKitNamespace("http://www.farent.solutions/hyperstripes/ns/dojo/1")
public class DojoRenderKit  extends AbstractRenderKit implements RenderKit {

    @Override
    public List<WebLibrary> getWebLibraries() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    /**
     * creates a model for the provided tagName. Each Model is different
     *
     * Also sets the ComponentHelper to ensure getComponentHelper does not return null
     * @param tagName
     * @return
     */
    @Override
    public ComponentModel getTagModelForTag( String tagName )
    {
        List<Class> classesWithAnnotation =
                ComponentAnnotationProcessor.getInstance().
                getClassesWithAnnotation( DojoModelProvider.class );
        ComponentModel model = null;
        for ( Class classWithAnnotation : classesWithAnnotation )
        {
            DojoModelProvider annotation = ( DojoModelProvider ) classWithAnnotation.getAnnotation(
                    DojoModelProvider.class );
            if( tagName.equalsIgnoreCase( annotation.value() ))
            {
                try
                {
                    model = ( ComponentModel ) classWithAnnotation.newInstance();
                }
                catch ( Exception ex )
                {
                    throw new RuntimeException( ex );
                }
                break;
            }
        }
        return model;
    }

    @Override
    public ComponentTagHandler getTagHandlerForTag( String tagName )
    {
        ComponentTagHandler tagHandler = null;
        List<Class> classesWithAnnotation =
                ComponentAnnotationProcessor.getInstance().
                getClassesWithAnnotation( DojoTagHandler.class );
        for ( Class classWithAnnotation : classesWithAnnotation )
        {
            DojoTagHandler annotation = ( DojoTagHandler ) classWithAnnotation.getAnnotation(
                    DojoTagHandler.class );
            if( tagName.equalsIgnoreCase( annotation.value() ))
            {
                try
                {
                    tagHandler = ( ComponentTagHandler ) classWithAnnotation.newInstance();
                }
                catch ( InstantiationException ex )
                {
                    throw new RuntimeException( ex );
                }
                catch ( IllegalAccessException ex )
                {
                    throw new RuntimeException( ex );
                }
            }
        }
        return tagHandler;
    }

}
